<template>
  <PerfectTableComponent
    :loadTableList="loadTableList"
    :changePageSize="changePageSize"
    :changePage="changePage"
    :removeQueryField="removeQueryField"
    :clearQuery="clearQuery"
    v-model:query="query"
    title="汽车工艺"
    ref="tableRef"
    :queryFormHeaders="queryFormHeaders"
    :table="table"
    :tableColumns="tableColumns"
    :showTableIndex="false"
    :operatorColumnWidth="300"
    :baseUrl="urls.API_URL_BACKSTAGE_MES_CARTECHNOLOGY"
    v-model:form="form"
    :formHeader="formHeader"
    dialogWidth="800px"
    @beforeAddRow="beforeAddRow"
    @afterLoadDataSuccessInEditRow="afterLoadDataSuccessInEditRow"
    @beforeFormSubmit="beforeFormSubmit"
    @afterSubmitSuccessOfAddRow="afterSubmitSuccessOfAddRow"
    :showEditButtonInToolBar="true"
  >
    <!-----------------------------表格自定义列1----------------------------------------->
    <!--所属栏目-->
    <template #table_column_carModelName="scope: any">
      {{ scope.row.carModel != null ? scope.row.carModel.name : '' }}
    </template>
    <!--对话框表单插槽底部，用于补充其他的插槽-->
    <template #dialog_form_bottom>
      <TechnologyDetails v-model="form.carTechnologyDetails" />
    </template>
  </PerfectTableComponent>
</template>

<script setup lang="ts">
import PerfectTableComponent from '@/components/base/table/PerfectTableComponent.vue';
import TechnologyDetails from '@/views/page/mes/cartechnology/TechnologyDetails.vue';
import { onMounted, provide, reactive, ref } from 'vue';
import useTableList from '@/api/hooks/useTableList';
import type { FormHeader, ResponseData, TableColumn } from '@/types/interface/base_interface';
import urls from '@/api/request/urls';
import { getDict } from '@/api/dict';
import type { CarTechnology, CarTechnologyDetail } from '@/types/interface/mes_interface';
import Request from '@/api/request/request';

// 表格工具必备参数
const { table, query, loadTableList, removeQueryField, clearQuery, changePageSize, changePage } = useTableList(
  urls.API_URL_BACKSTAGE_MES_CARTECHNOLOGY
);
// 将表格刷新函数传到子容器
provide('loadTableList', loadTableList);

//查询==========================================================
const queryFormHeaders = ref<FormHeader[]>([]);

//表格==========================================================
// 表格插件ref对象
const tableRef = ref();

//表格列
// 定义表格所有的列字段信息
const tableColumns = ref<TableColumn[]>([]);

//表单==========================================================
//表单对象
const form = ref<CarTechnology>({});
//表单元素类型
const formHeader = ref<FormHeader[]>([]);

const getCarModels = async () => {
  const resData: any = await new Request(urls.API_URL_BACKSTAGE_MES_CARMODEL + '/all').get();
  if (resData.success) {
    return resData.data;
  }
};
onMounted(async () => {
  //获取物料类别列表
  const carModels = await getCarModels();
  //定义优先级选项。放在mounted中执行时，要将引用改变量的属性也放进来。
  const enabledDict = await getDict('启用状态', 'boolean');
  queryFormHeaders.value = [
    // 查询表单
    {
      prop: 'name',
      label: '名称'
    },
    {
      prop: 'code',
      label: '编码'
    },
    {
      prop: 'carModelId',
      propAsName: 'carModel.id',
      label: '车型',
      type: 'selectObject',
      list: carModels,
      config: {
        valueKey: 'id',
        defaultProp: {
          value: 'id',
          label: 'name'
        }
      }
    },
    {
      prop: 'enabled',
      label: '是否启用',
      type: 'select',
      list: enabledDict
    }
  ];
  //表格对象
  tableColumns.value = [
    {
      label: '名称',
      prop: 'name',
      fixed: true,
      width: 200
    },
    {
      label: '编码',
      prop: 'code',
      fixed: true,
      width: 200
    },
    {
      label: '车型',
      prop: 'carModelName',
      type: 'custom',
      width: 100
    },
    {
      label: '启用状态',
      prop: 'enabled',
      width: 100,
      type: 'dict',
      dict: enabledDict
    },
    {
      label: '工艺步骤',
      prop: 'carTechnologyDetailNumber',
      width: 100
    },
    {
      label: '创建时间',
      prop: 'createTime',
      width: 180
    },
    {
      label: '最后编辑时间',
      prop: 'updateTime',
      width: 180
    }
  ];
  // 添加编辑表单
  formHeader.value = [
    // {
    //   prop: 'carTechnologyDetails',
    //   label: '工艺详情集合-隐藏',
    //   show: false
    // },
    {
      prop: 'carModelId',
      label: '车型',
      type: 'selectObject',
      list: carModels,
      config: {
        valueKey: 'id',
        defaultProp: {
          value: 'id',
          label: 'name'
        }
      },
      rules: [
        {
          required: true,
          message: '不能为空',
          trigger: 'blur'
        }
      ]
    },
    {
      label: '名称',
      prop: 'name',
      rules: [
        {
          required: true,
          message: '不能为空',
          trigger: 'blur'
        },
        {
          min: 1,
          max: 30,
          message: '长度在 1 到 30个字符',
          trigger: 'blur'
        }
      ]
    },
    {
      label: '编码',
      prop: 'code',
      rules: [
        {
          required: true,
          message: '不能为空',
          trigger: 'blur'
        },
        {
          min: 1,
          max: 30,
          message: '长度在 1 到 30个字符',
          trigger: 'blur'
        }
      ]
    },
    {
      label: '是否启用',
      prop: 'enabled',
      type: 'switch',
      list: enabledDict,
      rules: [
        {
          required: true,
          message: '不能为空',
          trigger: 'blur'
        }
      ]
    }
  ];
});
//在添加行的动作以前
const beforeAddRow = async () => {
  //传入初始值
  form.value = {
    enabled: true,
    carTechnologyDetails: [] //工艺详情集合
  };
};
//在编辑行读取后端记录后需要修改的地方
const afterLoadDataSuccessInEditRow = async (formEdit: CarTechnology) => {
  //重新封装表单对象的值
  if (formEdit.carModel != null) {
    formEdit.carModelId = formEdit.carModel.id;
  }
  if (formEdit.carTechnologyDetails !== undefined && formEdit.carTechnologyDetails.length > 0) {
    for (let carTechnologyDetailOfEach of formEdit.carTechnologyDetails) {
      if (carTechnologyDetailOfEach.material != null) {
        carTechnologyDetailOfEach.materialId = carTechnologyDetailOfEach.material.id;
      }
    }
  }
};
//在表单提交前对对象进行封装
const beforeFormSubmit = () => {
  if (form.value.carModelId != null) {
    form.value.carModel = {
      id: form.value.carModelId
    };
  }
  //由于后续在获取物料下拉框时就复制给工艺详情的material属性，因此这里取消
  //if (form.value.carTechnologyDetails !== undefined && form.value.carTechnologyDetails.length > 0) {
  //   for (let carTechnologyDetailOfEach of form.value.carTechnologyDetails) {
  //     if (carTechnologyDetailOfEach.materialId != null) {
  //       carTechnologyDetailOfEach.material = {
  //         id: carTechnologyDetailOfEach.materialId
  //       };
  //     }
  //   }
  // }
};
//在成功提交后
const afterSubmitSuccessOfAddRow = () => {
  //清空详情数组，这样在下一次添加时不会保留上次的值。
  if (form.value.carTechnologyDetails !== undefined) {
    form.value.carTechnologyDetails.length = 0;
  }
};
</script>

<style scoped></style>
